package cn.itcast.hotel.mq;

import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HotelListener {


    @Autowired
    private IHotelService iHotelService;

    /**
     * 监听 添加修改
     *
     * @param id 修改id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "hotel.inst"),
            exchange = @Exchange(name = "hotel.heima", type = ExchangeTypes.DIRECT),
            key = {"inst"}
    ))
    public void inst(Long id) {
        iHotelService.insertById(id);
        System.out.println("id = " + id);
    }

    /**
     * 监听删除
     *
     * @param id 删除的id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "hotel.del"),
            exchange = @Exchange(name = "hotel.heima", type = ExchangeTypes.DIRECT),
            key = {"del"}
    ))
    public void del(Long id) {
        iHotelService.deleteById(id);
        System.out.println("id = " + id);
    }
}